package com.wushi.lenovo.asia5b.app_config;

import com.mvp.base.model.ResultModel;
import com.mvp.dagger.modules.NetworkModule;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_model.other.CountModel;
import com.wushi.lenovo.asia5b.app_model.other.CountType;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subjects.PublishSubject;
import io.reactivex.subjects.Subject;

import static timber.log.Timber.d;

/**
 * Created by zhangyuncai on 2019/11/28.
 * 用户相关静态数据
 */
public class OrderRepository {
    private final static OrderRepository orderRepository =new OrderRepository();

    private final ApiService apiService;
    private int orderCount = 0;//主页面订单数量
    private Subject<Integer> orderCountSubject;
    private long lastGetOrderCountTime = 0;

    private static synchronized OrderRepository getInstance() {
        return orderRepository;
    }

    public OrderRepository() {
        orderCountSubject = PublishSubject.create();
        this.apiService = NetworkModule.getRetrofit().create(ApiService.class);
    }

    /**
     * 从后台更新数量并通知所有界面
     */
    public static void updateOrderCount() {
        getInstance().getPendingOrderCountByNet();
    }

    public static void setOrderCount(int orderCount) {
        getInstance().orderCount = orderCount;
        getInstance().orderCountSubject.onNext(orderCount);
    }

    /**
     * 订阅数据监听
     *
     * @return
     */
    public static Flowable<Integer> getOrderCountOB() {
        return getInstance().orderCountSubject.toFlowable(BackpressureStrategy.LATEST);
    }

    public static int getOrderCount() {
        return getInstance().orderCount;
    }

    /**
     * 获取 代付款数
     */
    private void getPendingOrderCountByNet() {
        if (!UserRepository.isLogin()) {
            d("--->>>>>>>>---没登录");
            return;
        }
        if ((System.currentTimeMillis() - lastGetOrderCountTime) < 1000) {
            return;
        }
        lastGetOrderCountTime = System.currentTimeMillis();
        apiService.getCount(CountType.Order_All_Count.getCountPath(), UserInfo.getMemberId())
                .map(new Function<ResultModel<CountModel>, Integer>() {
                    @Override
                    public Integer apply(ResultModel<CountModel> countModelResultModel) throws Exception {
                        return countModelResultModel.getData().getCount();
                    }
                })
                .subscribeOn(Schedulers.io())
                .subscribe(total -> {
                    orderCount = total;
                    //通知所有界面
                    if (!UserRepository.isLogin()) {
                        orderCount = 0;
                    }
                    orderCountSubject.onNext(orderCount);
                }, throwable -> {
                    throwable.printStackTrace();
                });

    }
}
